import os
from idlelib.help import copy_strip

from tool.ui import ui_instance
from tool.file_dialog import show_file_dialog
from network.sender import send_file
from psi.pre_hash import pre_hash
from conf import conf
from psi.client import pre_cuckoo,generate_t,split,generate_rsa,decrypto_rsa,crypto_t,transfer,intersection,crypto_result
from psi.server import generate_s,encrypto_s,decryto_t,combine_q,oprf,decrypto_result
import time
# 解析命令执行流程
def cmdLet(cmd,addr):
    base_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/') + "/../"
    if cmd == "request": # 请求求交
        ui_instance.log_message(f"server:收到请求")
        print(addr)
        ip = addr[0]
        result = show_file_dialog("收到来自+" + str(ip) + "的请求","请选择一个文件")
        if result:
            ui_instance.log_message(f"server:选择用于求交文件:{result}")
            ui_instance.log_message(f"server:预处理文件:{result}")
            start_time = time.time()
            pre_hash(result,"b")
            cost_time = (time.time() - start_time)*1000
            ui_instance.log_message(f"server:处理完成")
            ui_instance.log_message(f"server:预处理耗时{cost_time:.2f}ms")
            data = conf.read_json()
            data["ip"] = ip
            conf.write_json(data) # 保存ip
            send_file("accept","cmd")
            return
        else:
            ui_instance.log_message(f"server:取消求交选择")
            send_file("reject","cmd")
            return
    elif cmd == "reject": # 拒绝求交
        ui_instance.log_message(f"client:{str(addr[0])}拒绝了你的请求")
    elif cmd == "accept": # 同意求交
        ui_instance.log_message(f"client:{str(addr[0])}同意了你的请求")
        ui_instance.log_message(f"client:cuckoo预处理中...")
        start_time = time.time()
        result = pre_cuckoo.work()
        if result:
            cost_time = (time.time() - start_time)*1000
            ui_instance.log_message(f"client:cuckoo预处理完成")
            ui_instance.log_message(f"client:cuckoo耗时{cost_time:.2f}ms")
        else:
            ui_instance.log_message(f"client:cuckoo预处理失败")
            return
        ui_instance.log_message(f"client:生成t0,t1...")
        start_time = time.time()
        result = generate_t.work()
        if result:
            cost_time = (time.time() - start_time)*1000
            ui_instance.log_message(f"client:生成t0,t1,耗时{cost_time:.2f}ms")
        else:
            ui_instance.log_message(f"client:生成t0,t1失败")
            return
        start_time = time.time()
        ui_instance.log_message(f"client:进行矩阵数据拆分")
        split.work()
        ui_instance.log_message(f"client:矩阵数据拆分完成")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"client:拆分耗时{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"client:生成RSA密钥对")
        generate_rsa.work()
        ui_instance.log_message(f"client:生成RSA密钥对完成")
        ui_instance.log_message(f"client:发送密钥对")
        send_file(base_dir + "data/client/rsa/pk1.pem","file")
        send_file(base_dir + "data/client/rsa/pk2.pem", "file")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"client:密钥传输耗时{cost_time:.2f}ms")
        ui_instance.log_message(f"client:等待基础OT执行...")
        send_file("start_ot","cmd")
        return
    elif cmd == "start_ot": # 开始OT
        start_time=time.time()
        ui_instance.log_message(f"server:开始基础OT")
        ui_instance.log_message(f"server:生成随机字符串和选择")
        generate_s.work()
        ui_instance.log_message(f"server:随机字符串和选择生成完成")
        ui_instance.log_message(f"server:加密选择")
        encrypto_s.work()
        ui_instance.log_message(f"server:加密选择完成")
        cost_time = (time.time() - start_time) * 1000
        ui_instance.log_message(f"server:基础OT加密完成,耗时{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"server:发送加密密钥")
        send_file(base_dir+"data/server/key_enc.txt","file",to="client")
        ui_instance.log_message(f"server:发送完成")
        ui_instance.log_message(f"server:基础OT密钥分发完成,耗时{cost_time:.2f}ms")
        send_file("ot_key_done","cmd")
        return
    elif cmd == "ot_key_done": # 传输加密密钥完成
        start_time=time.time()
        ui_instance.log_message(f"client:接收加密密钥完成")
        ui_instance.log_message(f"client:开始解密密钥...")
        decrypto_rsa.work()
        ui_instance.log_message(f"client:解密密钥完成")
        ui_instance.log_message(f"client:开始加密矩阵...")
        crypto_t.work()
        ui_instance.log_message(f"client:加密矩阵完成")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"client:加密矩阵耗时{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"client:发送加密矩阵")
        transfer.transfer_128()
        ui_instance.log_message(f"client:发送加密矩阵")
        ui_instance.log_message(f"client:等待传输...")
        send_file(base_dir+"/data/client/position.bin","file",to="server")
        send_file("bucket_num","config")
        cost_time = (time.time() - start_time) * 1000
        ui_instance.log_message(f"client:传输耗时完成{cost_time:.2f}ms")
        send_file("transfer_done","cmd")

        return
    elif cmd == "transfer_done": # 传输完成
        start_time=time.time()
        ui_instance.log_message(f"server:接收完成")
        ui_instance.log_message(f"server:开始解密矩阵...")
        decryto_t.work()
        ui_instance.log_message(f"server:解密矩阵完成")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"server:解密矩阵耗时{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"server:合并矩阵...")
        combine_q.work()
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"server:合并矩阵完成,cost_time{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"server:进行oprf计算...")
        oprf.work()
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"server:oprf计算完成,cost_time{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message(f"server:发送结果...")
        send_file(base_dir+"data/server/p.bin","file",to="client")
        send_file(base_dir + "data/server/s.bin", "file", to="client")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"server:发送完成,等待结果...,耗时{cost_time:.2f}ms")
        send_file("result_done","cmd")
        return
    elif cmd == "result_done": # 结果完成
        start_time=time.time()
        ui_instance.log_message("client:开始交集计算...")
        intersection.work()
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"client:交集计算完成,耗时{cost_time:.2f}ms")
        start_time = time.time()
        ui_instance.log_message("client:加密结果...")
        crypto_result.work()
        ui_instance.log_message("client:传输结果...")
        send_file(base_dir+"data/client/result/result_en.txt","file",to="server/result")
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"client:传输完成,耗时{cost_time:.2f}ms")
        send_file("ok", "cmd")
        ui_instance.log_message("client:计算结束,结果保存在data/client/result/result.txt")

        return
    elif cmd == "ok": # 结束
        start_time=time.time()
        ui_instance.log_message("server:解密结果...")
        decrypto_result.work()
        cost_time = (time.time() - start_time)*1000
        ui_instance.log_message(f"server:解密结果完成,耗时{cost_time:.2f}ms")
        ui_instance.log_message("server:解密结束,结果保存在data/server/result/result.txt")
    else:
        print("unknown command")
